use axum::Json;
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
pub struct CusResponse<T> {
    pub data: Option<T>,
    code: u16,
    message: String,
}

impl<T> From<T> for CusResponse<T> {
    fn from(inner: T) -> Self {
        Self {
            data: Some(inner),
            code: 0,
            message: "ok".to_string(),
        }
    }
}

impl<T> CusResponse<T> {
    pub fn ok(data: T) -> Json<CusResponse<T>> {
        Json(Self {
            data: Some(data),
            code: 0,
            message: "ok".to_string(),
        })
    }
    pub fn error(message: String) -> Json<CusResponse<T>> {
        Json(CusResponse {
            data: None,
            code: 1,
            message,
        })
    }
    pub fn error_with_code(message: String, code: u16) -> Json<CusResponse<T>> {
        Json(CusResponse {
            data: None,
            code,
            message,
        })
    }
}
